package com.econ.powercloud.ems.timing.compute.strategy.process;

import com.econ.powercloud.clickhouse.dao.AnalogDataDAO;
import com.econ.powercloud.clickhouse.dao.StatusDataDAO;
import com.econ.powercloud.clickhouse.entity.AnalogData;
import com.econ.powercloud.clickhouse.entity.StatusData;
import com.econ.powercloud.ems.common.enums.ComputeType;
import com.econ.powercloud.ems.common.enums.DitTypeEnum;
import com.econ.powercloud.ems.dal.entity.CalCode;
import com.econ.powercloud.ems.dal.entity.EmsComputeConfig;
import com.econ.powercloud.ems.timing.compute.strategy.AbstractComputeCodeProcess;
import com.econ.powercloud.ems.util.TimeUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author : zjf
 * @Description : 某个点号在某个时间的值
 * @Date : 2022/11/25 16:52
 * @Modified by : zjf
 */
@Component
public class StartTimeComputeCodeProcess extends AbstractComputeCodeProcess {
    public StartTimeComputeCodeProcess() {
        setComputeUnitId(ComputeType.STARTTIME.getCode());
    }

    @Autowired
    private AnalogDataDAO analogDataDAO;
    @Autowired
    private StatusDataDAO statusDataDAO;

    @Override
    public Double compute(EmsComputeConfig config, Map<String, Double> hasCalculateVal,Long startTime) {
        if(CollectionUtils.isEmpty(config.getCalCode())){
            return null;
        }
        CalCode calCode = config.getCalCode().get(0);
        Long startDayTime = TimeUtil.getDayStartTime(new Date(startTime));
        if(DitTypeEnum.isYcData(calCode.getDitType())){
            List<AnalogData> analogDataList = analogDataDAO.selectByPidsAndTime(Lists.newArrayList(calCode.getPid()),true, startDayTime, startTime);
            if(CollectionUtils.isEmpty(analogDataList)){
                return null;
            }
            return Double.parseDouble(analogDataList.get(analogDataList.size()-1).getV());
        }
        if(DitTypeEnum.isYxData(calCode.getDitType())){
            List<StatusData> statusDataList = statusDataDAO.selectByPidsAndTime(Lists.newArrayList(calCode.getPid()),true, startDayTime, startTime);
            if(CollectionUtils.isEmpty(statusDataList)){
                return null;
            }
            return Double.parseDouble(statusDataList.get(statusDataList.size()-1).getV());
        }
        return null;
    }



}
